# TriggerSwitchD, executes commands to activate/deactive remote switches.
# (c) Copyright 2010 Pontus Strömdahl, AdhocSkill. 
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


module TriggerSwitchD
  
  #Loads up schedule with the actions in file
  def self.load_scheduled_actions(file,date=Date.today)
    rows = file.readlines
    rows.each do |code_line|
      yield(code_line, date)
    end
  end
  
  #Returns devices loaded from file
  def self.load_devices(file)
    devices = YAML::load(file)
    SwitchFactory.new(devices[0]["switches"][0].keys)
    devices.inject({}) do |result, device|
      result[device["name"]] = TriggerSwitchD::map_switches(device["switches"])
      result
    end
  end
  
  private 
  
  def self.map_switches(pre_switches) #:nodoc
    pre_switches.map do |switch|
      TriggerSwitchD::Switch.new(switch)
    end    
  end
end